import { Base } from "./base";

export class Admin extends Base {
  async login(name, pass) {
    const { data, token } = await this.$http("post", "/api/admin/login", {
      name,
      pass
    });
    return { data, token };
  }

  /**
   *
   * @param name
   * @param role_id
   * @param pageNum
   * @param pageSize
   * @returns {Promise<void>}
   */
  // eslint-disable-next-line camelcase
  async list({ name, role_id, pageNum = 1, pageSize = 10 }) {
    const { data = {} } = await this.$http("post", "/api/admin/admin", {
      name,
      role_id,
      pageNum,
      pageSize
    });
    return data;
  }

  /**
   *
   * @param id
   * @returns {Promise<boolean>}
   */
  async del(id) {
    const { errno } = await this.$http("post", "/api/admin/admin/del", { id });
    return errno === 0;
  }

  /**
   *
   * @param name
   * @param phone
   * @param email
   * @param ava
   * @param idcard
   * @param pass
   * @returns {Promise<void>}
   */
  async add({ name, phone, email, ava, idcard, pass }) {
    const { errno } = await this.$http("post", "/api/admin/admin/add", {
      name,
      phone,
      email,
      ava,
      idcard,
      pass
    });
    return errno === 0;
  }

  /**
   *
   * @param id
   * @param name
   * @param phone
   * @param email
   * @param ava
   * @param idcard
   * @param pass
   * @param role_id
   * @returns {Promise<void>}
   */
  // eslint-disable-next-line camelcase
  async update({ id, name, phone, email, ava, idcard, pass, role_id }) {
    const { errno } = await this.$http("post", "/api/admin/admin/update", {
      id,
      name,
      phone,
      email,
      ava,
      idcard,
      pass,
      role_id
    });
    return errno === 0;
  }

  /**
   *
   * @param id
   * @returns {Promise<void>}
   */
  async detail(id) {
    const { data } = await this.$http("post", "/api/admin/admin/detail", {
      id
    });
    return data;
  }

  /**
   *
   * @param token
   * @returns {Promise<void>}
   */
  async token(token) {
    const { data = {} } = await this.$http("post", "/api/admin/token", {
      token
    });
    return data;
  }

  /**
   *
   * @param fid
   * @param type
   * @returns {Promise<void>}
   */
  async menuList({ type = "tree", fid = 0 }) {
    const { data } = await this.$http("post", "/api/admin/menu", { fid, type });
    return data;
  }

  /**
   *
   * @param name
   * @param fid
   * @param fname
   * @param url
   * @param icon
   * @returns {Promise<void>}
   */
  async menuAdd({ name, fid, fname, url, icon }) {
    const { errno } = await this.$http("post", "/api/admin/menu/add", {
      name,
      fid,
      fname,
      url,
      icon
    });
    return errno === 0;
  }

  /**
   *
   * @param id
   * @returns {Promise<boolean>}
   */
  async menuDel(id) {
    const { errno } = await this.$http("post", "/api/admin/menu/del", { id });
    return errno === 0;
  }

  /**
   *
   * @param id
   * @param name
   * @param fid
   * @param url
   * @param icon
   * @param sort
   * @returns {Promise<boolean>}
   */
  async menuUpdate({ id, name, fid, url, icon, sort }) {
    const { errno } = await this.$http("post", "/api/admin/menu/update", {
      id,
      name,
      fid,
      url,
      icon,
      sort
    });
    return errno === 0;
  }

  /**
   *
   * @returns {Promise<[]>}
   */
  async roleList() {
    const { data = [] } = await this.$http("post", "/api/admin/role", {});
    return data;
  }

  /**
   *
   * @param name
   * @param sort
   * @returns {Promise<void>}
   */
  async roleAdd({ name, sort }) {
    const { errno } = await this.$http("post", "/api/admin/role/add", {
      name,
      sort
    });
    return errno === 0;
  }

  /**
   *
   * @param id
   * @returns {Promise<boolean>}
   */
  async roleDel(id) {
    const { errno } = await this.$http("post", "/api/admin/role/del", { id });
    return errno === 0;
  }

  /**
   *
   * @param id
   * @param name
   * @param sort
   * @returns {Promise<boolean>}
   */
  async roleUpdate({ id, sort, name, menu }) {
    const { errno } = await this.$http("post", "/api/admin/role/update", {
      id,
      name,
      sort,
      menu
    });
    return errno === 0;
  }
}
